package org.itboys.core.controller.web;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayCouponSubstract;
import org.itboys.core.entity.mongo.SundayCouponUserArea;
import org.itboys.core.entity.mysql.SundayAddProductOrder;
import org.itboys.core.entity.mysql.SundayCoupon;
import org.itboys.core.service.mysql.web.SundayCouponService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：贾琦磊
 * @Date：2018/8/27
 * @Description：优惠券后台管理
 */
@Controller
@RequestMapping("/sunday/web/coupon")
public class SundayCouponController extends BaseController{

    @Autowired
    private SundayCouponService couponService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "sunday/coupon/index";
    }
    @RequestMapping("/input")
    public String input(HttpServletRequest request,HttpServletResponse response
                        ,Model model
                        ,@RequestParam("id")Long id){
        SundayCoupon coupon = couponService.findOne(id);
        model.addAttribute("coupon",coupon);
        return "/sunday/coupon/input";
    }

    @RequestMapping(value = "/save")
    public void save(HttpServletRequest request, HttpServletResponse response
                     , @RequestParam(value = "id")Long id
                     , @RequestParam(value = "startDate")String startDate
                     ,@RequestParam(value = "endDate")String endDate
                     ,@RequestParam(value = "name")String name
                     ,@RequestParam(value = "couponWay")Integer couponWay
                     ,@RequestParam(value = "saleArea")Integer saleArea
                     ,@RequestParam(value = "userArea")List<Integer> userArea
                    , @RequestParam(value = "skuId")List<Long> skuIds
                    , @RequestParam(value = "baseAmount") String baseAmount
                    ,@RequestParam(value = "subAmount") String subAmount)throws Exception{
        SundayCouponSubstract couponSubstract=new SundayCouponSubstract(baseAmount,subAmount);
        SundayCoupon coupon=new SundayCoupon(name,startDate,endDate, couponWay,  saleArea, SundayCommonConstants.COUPON_STATE_USE);
        String msg="";
        if(!ListUtil.isNotNull(skuIds)){
            ResponseMessage.error(CommonConstants.FAIL_CODE,"请添加sku");
        }else if(!ListUtil.isNotNull(userArea)){
            ResponseMessage.error(CommonConstants.FAIL_CODE,"请选择用户范围");
        }
        couponService.save(id,coupon,couponSubstract,userArea,skuIds);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/select")
    public void select(HttpServletRequest request,HttpServletResponse response){
        PageResult<SundayCoupon> result = couponService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
    }

    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response,
                       @RequestParam(value = "id")Long id){
        couponService.delete(id);
        ResponseMessage.success();
    }
}
